#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/11/2 23:27
# @Author : 闫峰
# @File    : msku_sku.py
import requests
from requests.auth import HTTPBasicAuth

from utils.mysqlUtils.db_utils import DB

session = requests.session()
db = DB()


def Msku_Sku():
    """
    通过msku查询sku
    """
    url = "http://139.159.245.112:9201/product_ordersourcesku_1/_search"
    auth = HTTPBasicAuth("reader", "5t92xpAI5tznl")
    body = {
        "from": 0,
        "size": 10000,
        "query": {
            "bool": {
                "must": [
                    {
                        "term": {
                            "CustomerID": {
                                "value": 1
                            }
                        }
                    },
                    {
                        "terms": {
                            "OrderSourceID": [
                                "9948"
                            ]
                        }
                    }
                ],
                "filter": [
                    {
                        "term": {
                            "IsDelete": {
                                "value": 0
                            }
                        }
                    }
                ]
            }
        }
    }
    header = {"content-type": "application/json"}
    res = session.post(url=url, json=body, auth=auth)
    return res.json()

    # return res.json().get("hits").get("hits")[0].get("_source").get("SKU")


if __name__ == '__main__':
    def func1(date):
        if date.isdecimal() == True:
            return True
        else:
            return False


    res = Msku_Sku()["hits"]["hits"]
    for i in res:
        data = i["_source"]["SKU"]
        sku = i["_source"]["OrderSourceSKU"]
        res_sss = func1(sku)
        sql1 = """SELECT IsGroup FROM p_product WHERE CustomerId=1 AND sku=%s;"""
        is_group = db.execute_sql(sql1, (data,))["IsGroup"]
        is_group = int.from_bytes(is_group, "little")
        if is_group == 1 and res_sss == True :
            print(data)




